package com.jeeplus.modules.statistics.service;

import com.alibaba.fastjson.JSON;
import com.jeeplus.common.utils.RedisUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.database.datasource.annotation.DS;
import com.jeeplus.modules.statistics.model.constant.Constant;
import com.jeeplus.modules.statistics.mapper.CounterMapper;
import com.jeeplus.modules.statistics.model.domain.Counter;
import com.jeeplus.modules.sys.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: CounterService
 * Function:  窗口业务处理
 * Date:      2021/3/2 10:57
 *
 * @author hanKongbin
 * version    V1.0
 */
@DS("queuing")
@Service
@Slf4j
@RequiredArgsConstructor
public class CounterService {

    private final RedisUtils redisUtils;

    private final CounterMapper counterMapper;
    /**
     * 窗口号缓存有效时长
     */
    @Value("${statistics.cache.expiretime.counter}")
    private long expireTime;

    /**
     * 根据窗口号获取办理业务
     *
     * @param counterId 窗口id
     * @param areaId    区域id
     * @return
     */
    List<Counter> findSerialNoByCounterId(String counterId, String areaId) {
        if(StringUtils.isEmpty(counterId) || StringUtils.isEmpty(areaId)){
            return new ArrayList<>();
        }
        try {
            String key = Constant.COUNTER_AREA_COUNTER + areaId + "_" + counterId;
            if (redisUtils.hasKey(key)) {
                return JSON.parseArray(redisUtils.get(key).toString(), Counter.class);
            }
            List<Counter> data = counterMapper.selectSerialNoByCounterId(counterId, areaId);
            if (!CollectionUtils.isEmpty(data)) {
                redisUtils.set(key, JSON.toJSONString(data), expireTime);
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("读取窗口失败！窗口编号->{}，区域->{}", counterId, areaId);
        }
        return new ArrayList<>();
    }

    /**
     * 根据业务id获取窗口号
     *
     * @param serialNo 业务id
     * @param areaId   区域id
     * @return
     */
    List<String> findCounterNoBySerialNo(String serialNo, String areaId) {
        if(StringUtils.isEmpty(serialNo) || StringUtils.isEmpty(areaId)){
            return new ArrayList<>();
        }
        try {
            String key = Constant.COUNTER_AREA_SERIAL + areaId + "_" + serialNo;
            if (redisUtils.hasKey(key)) {
                return JSON.parseArray(redisUtils.get(key).toString(), String.class);
            }
            List<String> data = counterMapper.selectCounterNoBySerialNo(serialNo, areaId);
            if (!CollectionUtils.isEmpty(data)) {
                redisUtils.set(key, JSON.toJSONString(data), expireTime);
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("读取窗口失败！业务编号->{}，区域->{}", serialNo, areaId);
        }
        return new ArrayList<>();
    }


    /**
     * 根据区域id获取窗口号
     *
     * @param areaId
     * @return
     */
    public List<Counter> findAllByAreaId(String areaId) {
        try {
            List<Counter> data = counterMapper.selectAllByAreaId(areaId, "%"+UserUtils.getUser().getId()+"%");
            for (Counter datum : data) {
                datum.setCounterNo(datum.getCounterNo() + "号窗口");
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("读取窗口失败！区域->{}", areaId);
        }
        return new ArrayList<>();
    }

}
